﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_Admin_AdditionalParticipants : Form
    {
        public Pro_Admin_AdditionalParticipants()
        {
            InitializeComponent();
        }
        #region   随机生成学号
        private void Pro_Admin_AdditionalParticipants_Load(object sender, EventArgs e)
        {
            //生成随机数
            Random rand = new Random();
            var code = rand.Next(100000, 1000000);
            this.txt_UserName.Text = code.ToString();
            //文本框禁用
            this.txt_UserName.Enabled = false;
            var Garde = this.cho_Class.Text;
            Reader(Garde);
        }
        #endregion
        #region  注册验证
        /// <summary>
        /// 注册验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Opt_In_Click(object sender, EventArgs e)
        {
            var Sid = this.txt_UserName.Text;
            var Password = this.txt_Password.Text;
            var UserName = this.txt_Name.Text;
            var Age = this.txt_Age.Text;
            var Card = this.txt_ID.Text;
            var Birthday = this.dateTime.Text;
            var Address = this.txt_Address.Text;
            int Sex = -1;
            if (this.radio_man.Checked)
            {
                Sex = 1;
            }
            else if (this.radio_woman.Checked)
            {
                Sex = 2;
            }
            int Grade = -1;
            switch (this.cho_Class.Text)
            {
                case "大一":
                    Grade = 1;
                    break;
                case "大二":
                    Grade = 2;
                    break;
                case "大三":
                    Grade = 3;
                    break;
                default:
                    MessageBox.Show("年级输入值有误，请直接选择", "提示信息",
                                                       MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    break;
            }
            Legal();
            string str = @"Data Source = DESKTOP-28VSNS5;
                            Initial Catalog = Training_Project;
                            Uid = Lv;
                            Pwd = 123456";
            var coon = new SqlConnection(str);
            try
            {
                coon.Open();
                var Login_sql = string.Format(@"insert into Stu_Login(UserName,Password)
                                                values('{0}','{1}')", Sid, Password);
                var sql = string.Format(@"INSERT INTO Stu_StuInfo(Stu_StuInfo_Name,Stu_StuInfo_Age,Stu_StuInfo_Sex,
                                         Stu_StuInfo_Grade,Stu_StuInfo_Card,Stu_StuInfo_Birthday,Stu_StuInfo_Address)
                                        VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                                       UserName, Age, Sex, Grade, Card, Birthday, Address);
                var conm = new SqlCommand(Login_sql, coon);
                var comm = new SqlCommand(sql, coon);
                int num = conm.ExecuteNonQuery();
                int number = comm.ExecuteNonQuery();
                if (num > 0 && number > 0)
                {
                   DialogResult dr = MessageBox.Show("添加成功","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                   if (dr.Equals(DialogResult.OK))
                   {
                       this.Hide();
                   }
                }
                else
                {
                    MessageBox.Show("添加失败，请稍后再试","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库异常，请联系管理员" + ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  读取数据库中的数据
        public void Reader(string Grade)
        {
            string str = @"Data Source = DESKTOP-28VSNS5;
                            Initial Catalog = Training_Project;
                            Uid = Lv;
                            Pwd = 123456";
            var coon = new SqlConnection(str);
            try
            {
                coon.Open();
                var sql = "select * from Grade";
                SqlCommand comm = new SqlCommand(sql, coon);
                var dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    Grade = this.cho_Class.Items.Add(dr["Grede_Name"]).ToString();
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  注册检测是否合法
        public void Legal()
        {
            if (string.IsNullOrWhiteSpace(this.txt_Password.Text))
            {
                MessageBox.Show("密码不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Password.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Name.Text))
            {
                MessageBox.Show("姓名不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Name.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Age.Text))
            {
                MessageBox.Show("年龄不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Age.Focus();
            }
            else if (!this.radio_man.Checked && !this.radio_woman.Checked)
            {
                DialogResult dr = MessageBox.Show("性别不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr.Equals(DialogResult.OK))
                    this.radio_man.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.cho_Class.Text))
            {
                MessageBox.Show("年级不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.cho_Class.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_ID.Text))
            {
                MessageBox.Show("身份证不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_ID.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.dateTime.Text))
            {
                MessageBox.Show("出生日期不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.dateTime.Focus();
            }
            else if (string.IsNullOrWhiteSpace(this.txt_Address.Text))
            {
                MessageBox.Show("家庭住址不能为空", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Address.Focus();
            }
        }
        #endregion
        #region  键盘按下点击事件
        /// <summary>
        /// 键盘按下点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.Equals(13))
            {
                this.btn_Opt_In_Click(null, null);
            }
        }
        #endregion
    }
}
